package com.itheima.web.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.vo.CommentVo;
import com.itheima.domain.vo.MovementVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.db.UserService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class UserManager {

    @DubboReference
    private UserInfoService userInfoService;
    //分页查询用户数据
    public ResponseEntity findUsersByPage(Integer page, Integer pagesize) {
        PageBeanVo pageBeanVo = userInfoService.findByPage(page, pagesize);
        return ResponseEntity.ok(pageBeanVo);
    }

    //根据用户id查询数据
    public ResponseEntity findUserInfoById(Long userId) {
        UserInfo userInfo = userInfoService.findByUserId(userId);
        return ResponseEntity.ok(userInfo);
    }

    @DubboReference
    private MovementService movementService;
    //查询消息分页
    public ResponseEntity findMessages(Long uid, Integer state, Integer page, Integer pagesize) {
        //1.查询数据
        PageBeanVo pageBeanVo = movementService.findMovementByPage(uid, state, page, pagesize);
        //2.获得list
        List<Movement> movementList = (List<Movement>) pageBeanVo.getItems();
        List<MovementVo> movementVoList = new ArrayList<>();
        //3.判断
        if(CollectionUtil.isNotEmpty(movementList)){
            for (Movement movement : movementList) {
                //4.转换
                //3.1 查询用户信息
                UserInfo userInfo = userInfoService.findByUserId(movement.getUserId());
                //3.2 创建vo
                MovementVo vo = new MovementVo();
                vo.setUserInfo( userInfo ); //注意封装顺序
                vo.setMovement( movement ); //注意封装顺序
                //改造设置时间
                String date = DateUtil.formatDateTime(new Date(movement.getCreated()));
                vo.setCreateDate( date );
                movementVoList.add(vo);
            }
        }

        pageBeanVo.setItems(movementVoList);
        //5.返回
        return ResponseEntity.ok(pageBeanVo);
    }

    //根据id查询一个对象
    public ResponseEntity findMessageById(String movementId) {
        //查询数据
        Movement movement = movementService.findById(new ObjectId(movementId));

        //4.转换
        //3.1 查询用户信息
        UserInfo userInfo = userInfoService.findByUserId(movement.getUserId());
        //3.2 创建vo
        MovementVo vo = new MovementVo();
        vo.setUserInfo( userInfo ); //注意封装顺序
        vo.setMovement( movement ); //注意封装顺序
        //改造设置时间
        String date = DateUtil.formatDateTime(new Date(movement.getCreated()));
        vo.setCreateDate( date );
        return ResponseEntity.ok(vo);
}

    @DubboReference
    private CommentService commentService;
    //查询评论数据
    public ResponseEntity findCommentById(Integer page, Integer pagesize, String messageID) {
        //1.查询动态的评论数据commentService
        PageBeanVo pageBeanVo = commentService.findCommentByPage(new ObjectId(messageID), 2, page, pagesize);
        //2.获得集合
        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();
        List<CommentVo> commentVoList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(commentList)){
            for (Comment comment : commentList) {
                //3.查询用户数据
                UserInfo userInfo = userInfoService.findByUserId(comment.getUserId());
                //4.创建vo 封装
                CommentVo vo = new CommentVo();
                vo.setId( comment.getId().toHexString() );//评论id
                vo.setNickname( userInfo.getNickname()  );//昵称
                vo.setUserId( userInfo.getId() );//用户id
                vo.setContent( comment.getContent() );//内容
                vo.setCreateDate( DateUtil.formatDateTime( new Date(comment.getCreated()) ) );//时间
                commentVoList.add(vo);
            }
        }
        pageBeanVo.setItems( commentVoList );
        return ResponseEntity.ok(  pageBeanVo );
    }
}
